package com.ymatou.iapi.productmanager.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.tuple.Triple;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class SetNewProductCall  extends YmatouAutoTestCall {
	private static double picThreshold = 0.3;
	private static double txtThreshold = 0.3;
	
	private static String opurl ="/api/SellerProduct/SetNewProduct";

	public SetNewProductCall() {
		super(EnvSetup.getSystemUrl("productmanager"),opurl,"POST","JSON");
	}
	
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json");
	}

	
	public JSONObject call(List<Triple<String,Boolean,Boolean>> prodAndOverPicAndOverTxtList) throws JSONException{
		JSONObject requestBean = this.getJsonBean(null);
		
		JSONObject node = requestBean.getJSONArray("result").getJSONObject(0);
		
		for (int i=0;i<prodAndOverPicAndOverTxtList.size();i++){
			Triple<String,Boolean,Boolean>  tmp = prodAndOverPicAndOverTxtList.get(i);
			
			requestBean.getJSONArray("result").put(i,new JSONObject(node.toString()));
			requestBean.getJSONArray("result").getJSONObject(i).put("productId", tmp.getLeft());
			if (tmp.getMiddle()){
				requestBean.getJSONArray("result").getJSONObject(i).getJSONArray("pic").getJSONObject(0).put("score", picThreshold+0.01);
			}
			
			if (tmp.getRight()){
				requestBean.getJSONArray("result").getJSONObject(i).getJSONArray("text").getJSONObject(0).put("score", txtThreshold+0.01);
				
			}
			
		}
		
		return this.callAndGetReturnData(requestBean);
		
	}
	
	
	public JSONObject callNotOverThreshold(List<String> prodList) throws JSONException{
		List<Triple<String,Boolean,Boolean>> prodAndOverPicAndOverTxtList = new ArrayList<>();
		prodList.forEach(ele->{
			prodAndOverPicAndOverTxtList.add(Triple.of(ele, false, false));
		});
		
		return this.call(prodAndOverPicAndOverTxtList);
	}
	
	
	public static void main(String[] args) throws JSONException {
		String productid = ProductFactory.getOnlyProduct(3383,"",MapUtil.hashMap("Product->IsNew",false)).getProductId();
		new SetNewProductCall().callNotOverThreshold(Arrays.asList(productid));
	}
	
	
}